<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\game\controller;
use \cmf\controller\AdminBaseController;
use \think\Db;
/**
 * Description of GameListController
 *
 * @author Administrator
 */
class AdminResourceController extends AdminBaseController{
    private function sync($prefix){
        $storage = cmf_get_option('storage');
        if (empty($storage['type'])) {
            $storage['type'] = 'Local';
        }
        $cfg = $storage['storages'][$storage['type']];
        if (empty($cfg['driver'])) {
            $storageDriverClass = "\\cmf\\lib\\storage\\Local";
        } else {
            $storageDriverClass = $cfg['driver'];
        }
        $driver = new \plugins\alioss\lib\Alioss();//new $storageDriverClass($cfg);
        $rs = $driver->getFileList('preload/'.$prefix);
        return $rs;
    }
    
//    private function getStyle($style){
//        $storage = cmf_get_option('storage');
//        if (empty($storage['type'])) {
//            $storage['type'] = 'Local';
//        }
//        $cfg = $storage['storages'][$storage['type']];
//        $rs['domain'] = $cfg['domain'];
//        $rs['style_separator'] = $cfg['style_separator'];
//        if(isset($cfg[$style])){
//            $rs['style'] = $cfg[$style];
//        }else{
//            $rs['style'] = $cfg['styles_watermark'];
//        }
//        return $rs;
//    }
    
    public function index(){
        $type = $this->request->param('type','icon');
        $pData = Db::name('game_resource')->field(['res.*','a.file_path','a.file_size'])->alias('res')->join('asset a','res.fileid=a.id')->where(['res.filetype'=>$type])->order('id desc')->paginate(20);
        $res = $pData->items();
        foreach ($res as $k=>$val){
            $val['file_path'] = cmf_get_image_preview_url($val['file_path'],'icon');
            $res[$k] = $val;
        }
        $data = ['assets'=>$res,'page'=>$pData->render(),'type'=>$type];
        $this->assign($data);
        return $this->fetch();
    }
    
    public function add(){
        return $this->fetch();
    }
    
    public function del(){
        $id = $this->request->param('id/d');
        Db::name('game_resource')->where('id',$id)->delete();
        $this->success('删除成功');
    }
    
    public function edit(){
        $id = $this->request->param('id/d');
        if($this->request->isAjax()){
            $data['id'] = $id;
            $data['filename'] = $this->request->param('filename');
            $data['price'] = $this->request->param('price');
            $data['skin_id'] = $this->request->param('skin_id');
            $data['enable_i'] = $this->request->param('enable_i')=='on';
            Db::name('game_resource')->update($data);
            $this->success('修改成功');
        }else{
            $data = Db::name('game_resource')->find($id);
        }
        $this->assign($data);
        return $this->fetch();
    }
    
    public function addRes(){
        $id = $this->request->param('resid');
        $type = $this->request->param('type');
        $res = Db::name('asset')->where('file_key',$id)->find();
        $data = ['fileid'=>$res['id'],'filetype'=>$type,'filename'=>'新增','enable_i'=>0,'file_path'=>$res['file_path']];
        Db::name('game_resource')->insert($data);
        $this->success('添加成功');
    }
}
